import { User } from "../../../../modeal/user.js"
import { MyDate } from "../../../../utils/util.js"
const user = new User()
Page({

  data: {
    orderDetail: {},  // 订单总详情
    orderLists: [],    //订单详情的菜品列表
    posttime: ""

  },

  onLoad: function (options) {
    let oid = options.oid
    let currentIndex = options.currentIndex
    this.getOrderDetail(oid, currentIndex)  //获取订单详情

  },

  // 获取订单详情
  getOrderDetail (oid, currentIndex) {
    user.getOrderDetail(oid).then(res => {
      if (currentIndex == 1) {
        let obj = res.details
        let orderLists = []
        for (const key in obj) {
          obj[key].forEach(item => {
            orderLists.push(item)
          });
        }
        this.setData({ orderLists })
      }
      else {
        this.setData({ orderLists: res.details[1] })
      }
      this.setData({ orderDetail: res })
      this.transformTime(res.posttime)   // 转换
    })
  },

  // 时间转换
  transformTime (time) {
    let posttime = MyDate(time)
    this.setData({ posttime })
  },

  // 取消订单
  cancelOrder () {
    let oid = this.data.orderDetail.oid
    wx.showModal({
      content: '您确定要取消当前订单么',
      success: res => {
        if (res.confirm) {
          user.cancelOrder(oid).then(res => {
            wx.showToast({
              title: "SUCCESS",
              icon: 'success',
            })
            setTimeout(() => {
              wx.navigateBack({ data: 1 })
            }, 2000);
          })
        }
      }
    })
  },

  // 去支付
  goPay () {
    // 选择支付方式‘
    wx.showActionSheet({
      itemList: ['余额支付', '微信支付'],
      success: res => {
        let paytype = res.tapIndex + 1
        this.pay(paytype)
      },
      fail: err => { console.log(err) }
    })
  },

  // pay
  pay (paytype) {
    let eatInOid = this.data.orderDetail.oid
    let userinfo = wx.getStorageSync('userinfo')
    let payAmount = this.data.orderDetail.pay_amount
    let isEnoughMoney = userinfo.balance - payAmount
    if (paytype == 1) {
      isEnoughMoney < 0 ? wx.showToast({
        title: '余额不足'
      }) : this.payForBalance(eatInOid)
    } else {
      this.payForWx(eatInOid)
    }
  },
  // 余额支付
  payForBalance (eatInOid) {
    user.payForBalance(eatInOid).then(res => {

      // 清除缓存
      wx.removeStorageSync('eatInOid')
      wx.removeStorageSync('deskid')
      wx.redirectTo({
        url: '/pages/shopping/pays/pays'
      })
    })
  },

  // 微信支付
  payForWx (eatInOid) {
    let amount = 0
    user.wxPay(amount, eatInOid).then(res => {
      wx.showModal({
        content: "上线对接"
      })
    })
  },

  // 微信发起支付
  payment () {
    wx.requestPayment({
      timeStamp: String(data.timeStamp),
      nonceStr: data.nonceStr,
      package: data.package,
      signType: data.signType,
      paySign: data.paySign,
      success: function (res) {
        // 清除缓存数据
        wx.removeStorageSync('eatInOid')
        wx.removeStorageSync('deskid')
        wx.showModal({
          title: '订单提示',
          content: '支付成功',
          showCancel: false,
          success: res => {
            wx.redirectTo({
              url: '/pages/shopping/pays/pays'
            });
          },
          fail: err => {
            wx.showModal({
              title: '订单提示',
              content: '订单支付取消',
              showCancel: false,
              success: function () {
                wx.redirectTo({
                  url: "/pages/user/myOrder/myOrder"
                });
              }
            });
          },
        });
      }
    })
  }
})